home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8004 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: Thu, 29 Feb 96 19:08:46 GMT
  6. Organization: none
  7. Message-ID: <825620926snz@genesis.demon.co.uk>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com> <31331a38.54160408@nntp.ix.netcom.com> <1996Feb28.195423.10465@isac.hces.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <1996Feb28.195423.10465@isac.hces.com>
  15.            gg@isac.hces.com "Greg Goodrich" writes:
  16.  
  17. >Mike Rubenstein (miker3@ix.netcom.com) wrote:
  18. ...
  19. >: In many implementations pointer to char and pointer to array of 10
  20. >: char have the same representation and this will work properly, but
  21. >: this is not required by the standard.
  22. >
  23. >I would like to see an example of how this could be implemented to fail.
  24.  
  25. While I don't have firsthand knowledge of a practical example it is easy
  26. to create a theoretical one.
  27.  
  28. >They both point to the exact same memory address, the only difference
  29. >being the datatype of the value, which can be type casted if necessary.
  30.  
  31. And that is the key point. Pointers with different types can have different
  32. representations, even different sizes - sizeof(int *) may be different
  33. to sizeof(char *)
  34.  
  35. >If you have an array, the address of the array is also the address of
  36. >the first member of the array,
  37.  
  38. both short and long types can represent the value 1 but on many systems
  39. they represent it in different ways so a cast between short and long can
  40. result in a format change (in that case typically adding or removing
  41. leading bits). The same principle applies to different pointer types -
  42. 2 pointers may in some sense refer to the same address but may do so using
  43. different representations.
  44.  
  45. The FAQ cites examples of this in section 5.17.
  46.  
  47. >because the array itself is not a
  48. >pointer, but a reference.  The same goes for a structure.  The address
  49. >of a struct is the same as the address of the first member of the
  50. >struct, but of different data type.
  51.  
  52. The problem here is that you can't compare pointers to different types
  53. directly in C so saying they point to the same address is meaningless
  54. and can't be validated one way or the other. You can of course cast the
  55. pointers to a common type but you are then potentially changing the
  56. pointer representations.
  57.  
  58. > One is pointer to struct and the
  59. >other is pointer to whatever the first member is declared as, but they
  60. >are both pointers, and therefore are the same size.
  61.  
  62. Not in general. The standard says (6.1.2.5)
  63.  
  64. "A pointer to void shall have the same representation and alignment
  65.  requirements as a pointer to a character type. Similarly, pointers to
  66.  qualified or unqualified versions of compatible types shall have the same
  67.  representation and alignment requirements. Pointers to other types
  68.  need not have the same representation and alignment requirements."
  69.  
  70.  
  71. char *  and  char (*)[10]  are *not* compatible types.
  72.  
  73. -- 
  74. -----------------------------------------
  75. Lawrence Kirby | fred@genesis.demon.co.uk
  76. Wilts, England | 70734.126@compuserve.com
  77. -----------------------------------------
  78.